package controller.employee;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import lib.MyUtils;
import model.Employee;
import view.employee.EmployeeMainPanel;
import controller.DoCheck;
import dao.EmployeeDAO;

public class EmployeeControl implements ActionListener, MouseListener {
	private EmployeeDAO employeeDAO;
	private EmployeeMainPanel emp;

	public EmployeeControl(EmployeeMainPanel emp) {
		this.emp = emp;
		employeeDAO = new EmployeeDAO();
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == emp.getBtnEmAdd()) {
			doSave(event);
		} else if (event.getSource() == emp.getBtnEmEdit()) {
			emp.getBtnEmUpdate().setEnabled(true);
		} else if (event.getSource() == emp.getBtnEmDelete()) {
			doDelete(event);
		} else if (event.getSource() == emp.getBtnEmUpdate()) {
			doUpDate(event);
		} else if (event.getSource() == emp.getBtnEmSearch()) {
			doSearch(event);
		} else if (event.getSource() == emp.getBtnEmCancel()) {
			doReset(event);
		}
	}

	public void doLoad(ActionEvent event) {
		MyUtils.doLoadEmployee(emp);
	}

	@SuppressWarnings("static-access")
	public void doSave(ActionEvent event) {
		Employee employee = new Employee();
		DoCheck check = new DoCheck();

		String id = emp.getTxtEmId().getText();
		employee.setId(id);
		employee.setName(emp.getTxtEmName().getText());
		employee.setEmpsex(emp.getCbbEmSex().getSelectedItem().toString());
		employee.setEmpBirth(emp.getDateEmBirthday().getDate().toString());
		employee.setEmpPerId(emp.getTxtEmPersonalID().getText());
		String phone = emp.getTxtEmPhone().getText();
		employee.setPhone(phone);
		String email = emp.getTxtEmEmail().getText();
		employee.setEmail(email);
		employee.setAddress(emp.getTxtaEmAddress().getText());
		employee.setPassword(emp.getTxtEmPassword().getText());

		if (check.doCheckEmail(email) && check.doCheckPerId(id)
				&& check.doCheckPhone(phone)
				&& employeeDAO.insertEmployee(employee)) {
			JOptionPane.showMessageDialog(null, "Insert Employee successfully");
			doLoad(event);
		} else {
			JOptionPane.showMessageDialog(null, "Insert Employee fail");
		}
	}

	public void doDelete(ActionEvent event) {
		int check = JOptionPane.showConfirmDialog(null,
				"Do you want to delete?", "Delete", JOptionPane.YES_NO_OPTION);
		if (check == JOptionPane.YES_OPTION) {
			String EmpId = (String) emp.getTblEmListem().getValueAt(
					emp.getTblEmListem().getSelectedRow(), 0);
			employeeDAO.deleteEmployee(employeeDAO.searchEmployee(EmpId));
			JOptionPane.showMessageDialog(null, "Delete Employee successfully");
			doLoad(event);
		} else if (check == JOptionPane.CANCEL_OPTION) {
		}
	}

	public void doReset(ActionEvent event) {
		emp.getTxtEmName().setText("");
		emp.getTxtaEmAddress().setText("");
		emp.getTxtEmEmail().setText("");
		emp.getTxtEmPassword().setText("");
		emp.getTxtEmPersonalID().setText("");
		emp.getTxtEmPhone().setText("");
		emp.getTxtEmSearch().setText("");

		emp.getBtnEmEdit().setEnabled(false);
		emp.getBtnEmUpdate().setEnabled(false);
		emp.getBtnEmDelete().setEnabled(false);
	}

	public void doUpDate(ActionEvent event) {
		Employee employee = new Employee();
		String id = (String) emp.getTblEmListem().getValueAt(
				emp.getTblEmListem().getSelectedRow(), 0);
		employee = employeeDAO.searchEmployee(id);

		employee.setName(emp.getTxtEmName().getText());
		employee.setPhone(emp.getTxtEmPhone().getText());
		employee.setEmpPerId(emp.getTxtEmPersonalID().getText());
		employee.setEmail(emp.getTxtEmEmail().getText());
		employee.setAddress(emp.getTxtaEmAddress().getText());
		employee.setPassword(emp.getTxtEmPassword().getText());
		employee.setEmpsex(emp.getCbbEmSex().getSelectedItem().toString());

		if (employeeDAO.updateEmployee(employee) && employee != null) {
			JOptionPane.showMessageDialog(null, "Update successfully");
			doLoad(event);

		} else if (id == null) {
			JOptionPane.showMessageDialog(null, "Update fail");
		}

	}

	public void doSearch(ActionEvent event) {
		String key = emp.getTxtEmSearch().getText();
		DefaultTableModel dtm = new DefaultTableModel();
		dtm.addColumn("ID");
		dtm.addColumn("Name");
		dtm.addColumn("Sex");
		dtm.addColumn("Birthday");
		dtm.addColumn("PersonId");
		dtm.addColumn("Phone");
		dtm.addColumn("Email");
		dtm.addColumn("Address");
		for (Employee employee : employeeDAO.showAllEmployeeByAll(key)) {
			dtm.addRow(new Object[] { employee.getId(), employee.getName(),
					employee.getEmpsex(), employee.getEmpBirth(),
					employee.getEmpPerId(), employee.getPhone(),
					employee.getEmail(), employee.getAddress() });
		}
		emp.getTblEmListem().setModel(dtm);

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		String EmpId = (String) emp.getTblEmListem().getValueAt(
				emp.getTblEmListem().getSelectedRow(), 0);
		Employee employee = (Employee) employeeDAO.searchEmployee(EmpId);
		emp.getTxtEmPersonalID().setText(employee.getEmpPerId());
		emp.getTxtEmEmail().setText(employee.getEmail());
		emp.getTxtEmName().setText(employee.getName());
		emp.getTxtEmPhone().setText(employee.getPhone());
		emp.getTxtaEmAddress().setText(employee.getAddress());
		emp.getTxtEmId().setText(employee.getId());
		emp.getTblEmListem().repaint();
		emp.getTblEmListem().revalidate();
		emp.getCbbEmSex().setSelectedItem(employee.getEmpsex());
		emp.getBtnEmEdit().setEnabled(true);
		emp.getBtnEmDelete().setEnabled(true);
		emp.getTxtEmPassword().setText(employee.getPassword());
	}

	@Override
	public void mouseEntered(MouseEvent e) {

	}

	@Override
	public void mouseExited(MouseEvent e) {

	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {

	}

}